package com.example.administrator.swredeem.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.example.administrator.swredeem.R;
import com.example.administrator.swredeem.appBase.Config;
import com.example.administrator.swredeem.net.HttpXutils;
import com.example.administrator.swredeem.net.MyCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DianhuoGbActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.tx_bjcj)
    TextView txBjcj;
    @BindView(R.id.ll_bjcj)
    LinearLayout llBjcj;
    @BindView(R.id.tx_bjlx)
    TextView txBjlx;
    @BindView(R.id.ll_bjlx)
    LinearLayout llBjlx;
    @BindView(R.id.et_bjxh)
    EditText etBjxh;
    @BindView(R.id.et_bjbh)
    EditText etBjbh;
    @BindView(R.id.tx_yhlb)
    TextView txYhlb;
    @BindView(R.id.ll_yhlb)
    LinearLayout llYhlb;
    @BindView(R.id.tx_dhr)
    TextView txDhr;
    @BindView(R.id.ll_dhr)
    LinearLayout llDhr;
    @BindView(R.id.tx_dhsj)
    TextView txDhsj;
    @BindView(R.id.ll_dhsj)
    LinearLayout llDhsj;
    private OptionsPickerView pickerView;
    private TimePickerView timePickerView;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dianhuo_gb);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        list = new ArrayList();
        llBjcj.setOnClickListener(this);
        llBjlx.setOnClickListener(this);
        llDhr.setOnClickListener(this);
        llDhsj.setOnClickListener(this);
        llYhlb.setOnClickListener(this);
    }

    public void goSubmit(View v) {

        Map<String, String> map = new HashMap<>();
        map.put("workid", getIntent().getStringExtra("workid"));//工单id
        map.put("source", "1");//来源
        map.put("meterfac", "1");//表具厂家
        map.put("metertype", "1");//表具类型
        map.put("metermodel", "");//表具型号
        map.put("meterno", "");//表号
        map.put("usertype", "");//用户类别
        map.put("ignition", "");//点火人
        map.put("ignitime", txDhsj.getText().toString());//点火时间
        map.put("operator", "sysadmin");//操作员
        getdata(map);
    }

    public void getdata(Map map) {

        HttpXutils.getData(Config.DhGbTj, map, new MyCallback() {

            @Override
            public void onSuccess(String result) {
                Log.d("TAG", "onSuccess: " + result);
                try {
                    JSONObject obj = new JSONObject(result);
                    if (obj.get("recode").equals("0000")) {
                        Toast.makeText(DianhuoGbActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(DianhuoGbActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(DianhuoGbActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("TAG", "onError: " + ex.getMessage() + isOnCallback);
                Toast.makeText(DianhuoGbActivity.this, "网络错误，请联系管理员", Toast.LENGTH_SHORT).show();
            }
        });
    }


    //单项选择弹出框
    public void ShowDig(final TextView textView) {
        pickerView = new OptionsPickerView.Builder(DianhuoGbActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                // 获取选中项的文本
                String data = list.get(options1).toString();
                textView.setText(data);
                Log.d("TAG", "onOptionsSelect: " + data);
            }
        }).build(); // 生成OptionsPickerView
        pickerView.setPicker(list); // 一级选择器
        pickerView.show(); // 显示

    }

    @Override
    public void onClick(View v) {
        list.clear();
        switch (v.getId()) {
            case R.id.ll_bjcj:
                list.add("威星");
                list.add("先锋");
                list.add("金卡");
                ShowDig(txBjlx);
                break;
            case R.id.ll_bjlx:
                list.add("气量表");
                list.add("金额表");
                list.add("机械气量表");
                ShowDig(txBjlx);
                break;
            case R.id.ll_yhlb:
                list.add("用户1");
                list.add("用户2");
                list.add("用户3");
                ShowDig(txYhlb);
                break;
            case R.id.ll_dhr:
                list.add("点火人1");
                list.add("点火人2");
                list.add("点火人3");
                ShowDig(txDhr);
                break;
            case R.id.ll_dhsj:
                showDigTime();
                break;

        }
    }

    public void showDigTime() {

        boolean[] type = new boolean[]{true, true, true, true, true, true};
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(2000, 11, 31);//最初时间
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();//当前选择时间
        endDate.set(2150, 11, 31);//结束时间
        selectedDate.add(Calendar.DAY_OF_MONTH, 1);
        TimePickerView pvTime = new TimePickerView.Builder(DianhuoGbActivity.this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                txDhsj.setText(getTime(date));
            }
        }).setType(type).setContentSize(20).setDate(startDate).setRangDate(selectedDate, endDate).build();

        pvTime.show();

    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

}
